package com.group10.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.group10.entity.PageBean;
import com.group10.entity.Result;
import com.group10.entity.Show;
import com.group10.entity.User;
import com.group10.mapper.ShowMapper;
import com.group10.service.ShowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShowServiceImpl implements ShowService {

    @Autowired
    private ShowMapper showMapper;


    @Override
    public Result<?> getAllShow(Integer pageNum, Integer pageSize) {
        PageBean<Show> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper
        List<Show> users = showMapper.getAllShow();
        Page<Show> p = (Page<Show>) users;
        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return Result.success(pb);
//        return Result.success(showMapper.getAllShow());
    }

    @Override
    public Result<?> getShowById(Integer id) {
        Show show = showMapper.getShowById(id);
        if (show == null){
            return Result.error("不存在的展品");
        }
        return Result.success(show);
    }

    @Override
    public Result<?> getShowByClassify(String classify) {
        return Result.success(showMapper.getShowByClassify(classify));
    }

    @Override
    public Result<?> insertShow(String detail, String url, String classify) {
        if (showMapper.insertShow(detail,url,classify) > 0){
            return Result.success();
        }
        return Result.error("新建文化展品失败");
    }

    @Override
    public Result<?> deleteShow(Integer id) {
        if (showMapper.deleteShow(id) > 0){
            return Result.success();
        }
        return Result.error("删除文化展品失败");
    }
}
